package com.lichuandi.springbootdemo1.service;

import com.alibaba.fastjson.JSON;
import com.lichuandi.springbootdemo1.entity.NotifyDto;
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.security.GeneralSecurityException;
import java.util.Map;

/**
 * @author Lichuandi
 * @version 1.0
 * @program : springbootdemo1
 * @ClassName NativePayServiceImpl
 * @description :
 * @create: 2025-01-03 15:59
 **/
@Service
public class NativePayServiceImpl implements NativePayService{

    private String apiV3key = "123456"; // v3密钥
    @Override
    public Map<String,String> payNotify(@RequestBody NotifyDto notifyDto) {
        Map<String,String> res = null;
        try {
            String json =  new AesUtil(apiV3key.getBytes()).decryptToString(
                    notifyDto.getResource().getAssociated_data().getBytes(),
                    notifyDto.getResource().getNonce().getBytes(),
                    notifyDto.getResource().getCipherText());
            String outTradeNo = JSON.parseObject(json,Map.class).get("out_trade_no").toString();
            System.out.println("支付商户订单号成功："+outTradeNo);
        }catch (GeneralSecurityException e){
            e.printStackTrace();
            res.put("code","500");
            res.put("message","解密失败");
        }
        return res;
    }
}
